package com.joke.auto;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.jdbc.mysql.MysqlLocalConnect;
import com.jdbc.mysql.MysqlServerConnect;
import com.joke.util.Const;
import com.joke.util.DateUtil;
import com.joke.util.DownloadUtil;
import com.joke.util.HttpClientGet;
import com.joke.util.StringUtil;

public class FetchHot {
	private static final Logger logger = LoggerFactory.getLogger(FetchHot.class);

	private static String qiubaiHot = "https://www.qiushibaike.com/8hr/page/(*)/";
	private static String baisiHot = "http://www.budejie.com/(*)";
	
	private static String qbCookie = "_ga=GA1.2.1549135057.1520475101; _qqq_uuid_=\"2|1:0|10:1520475372|10:_qqq_uuid_|56:ZTFkNmFlZjhkYzA2N2FiYjE4NmE1ZjJiN2U1MDhlYzY2OWI2ZGE4YQ==|e5a0b4a20205eef24dff2b9e5319748f9db7583be0a947b84c15fae472c4ec16\"; Hm_lvt_18a964a3eb14176db6e70f1dd0a3e557=1520475389; Hm_lvt_743362d4b71e22775786fbc54283175c=1521187836,1521691216; UM_distinctid=1624bdd49a1eb-0505b761b2d4ef-2b0c3866-3d10d-1624bdd49a2276; CNZZDATA1262682741=1214074336-1521689933-%7C1521702324; _gid=GA1.2.1851855991.1521799206; _xsrf=2|0e1e5b4a|5f22a9ab71ebe02fe69d1563b23e5de9|1521816186; Hm_lvt_2670efbdd59c7e3ed3749b458cafaa37=1521167928,1521534546,1521799206,1521816186; Hm_lpvt_2670efbdd59c7e3ed3749b458cafaa37=1521816186";
	private static String bsCookie = "_ga=GA1.2.605736126.1520487304; bfd_g=a0c202420a01680a00003d810001a5ee5aa0cb88; bu=\"2018-03-21 14:42:29\"; baifen_cookie=065085762cd311e8bf80b82a72dd382c; _gid=GA1.2.545856828.1521797615; tma=43102518.25831978.1520487303646.1521691124423.1521797614835.7; Hm_lvt_7c9f93d0379a9a7eb9fb60319911385f=1521614433,1521691124,1521797615,1521816256; Hm_lpvt_7c9f93d0379a9a7eb9fb60319911385f=1521816256; bfd_s=43102518.86695166.1521816255720; tmc=1.43102518.5988818.1521816255722.1521816255722.1521816255722; tmd=56.43102518.25831978.1520487303646.";

	private Date day = new Date();
	private String date = DateUtil.formatString(day, "yyyyMM");
	private String Y = DateUtil.formatString(day, "yyyy");
	private String M = DateUtil.formatString(day, "MM");
	private String D = DateUtil.formatString(day, "dd");
	private String ymd = Y+"/"+M+"/"+D;
	String writeHotPath=Const.fabu+date+Const.localHot;
	public static void main(String[] args) {
//		fetch();	
    }

	public void fetch() {
		Map<String, String> hotMap = new HashMap<>();
		qiubaiHot(hotMap);
		baisiHot(hotMap);
		logger.info("抓取的<热门>条数："+hotMap.size());
		//保存数据库  本地和服务器
    	MysqlServerConnect.insertHot(hotMap);
    	//保存入库热门
    	DownloadUtil.writeImage(writeHotPath,hotMap);
/*		System.out.println(hotMap.size());
		for (String key : hotMap.keySet()) {
			System.out.println(key);
			System.out.println(hotMap.get(key));
		}*/
	}
	
	private Map<String, String> qiubaiHot(Map<String, String> hotMap) {
		for (int i = 1; i < 2; i++) {
			String contentHTML = getContent(qiubaiHot,qbCookie,i);
			parseQBHotContent(contentHTML,hotMap);
		}
		return hotMap;
	}
	
	private Map<String, String> baisiHot(Map<String, String> hotMap) {
		for (int i = 1; i < 2; i++) {
			String contentHTML = getContent(baisiHot,bsCookie,i);
			parseBSHotContent(contentHTML,hotMap);
		}
		return hotMap;
	}

	private String getContent(String url,String cookie, int i) {
		HttpClientGet.createDefaultHttpClient();
		Map<String, String> header = new HashMap<>();
		header.put("Cookie",cookie);
		HttpClientGet.setHeader(header);
		if(i==0) return null;
		else if(i>0) url = url.replace("(*)", String.valueOf(i));
		String contentHTML = HttpClientGet.sendHttpGet(url);
		return contentHTML;
	}

	private Map<String, String> parseQBHotContent(String contentHTML,Map<String, String> hotMap) {
		if(contentHTML==null||"".equals(contentHTML)) return hotMap;
		Document doc = Jsoup.parse(contentHTML);  
		Elements links = doc.select("[id^=qiushi_tag_]");  
		String linkOuterH = null;  
		Document doc1 = null; 
		Elements links1 = null; 
		String content = null; 
		Elements imageEle = null;   
		String imagePath = null; 
    	MysqlLocalConnect.getConnect();
		for (Element element : links) {
			linkOuterH = element.outerHtml();  
			doc1 = Jsoup.parse(linkOuterH);  
			links1 = doc1.select("[class=content]");  
			content = links1.text().trim();
			imageEle = doc1.select("[src$=.jpg]");  
			imagePath = imageEle.attr("src").trim();
			String imageName = "";
			//下载文件  并保存到本地，下载的时候去重这样保证下载的都是新数据！后面上传到服务器的时候不用再去重
			if(StringUtil.isNotEmpty(imagePath)) {
				if(!imagePath.startsWith("http:")) {
					imagePath = "http:"+imagePath;
				}
				imageName = imagePath.substring(imagePath.lastIndexOf("/")+1, imagePath.length());
			}
			if (StringUtil.check(content)&&MysqlLocalConnect.query(content, imageName)) {
				if(StringUtil.isNotEmpty(imagePath)) {
					boolean a= DownloadUtil.downloadImg(imagePath,Const.hotPath+ymd+Const.o+imageName);
					if(a) hotMap.put(StringUtil.reSpecial(content),imageName);
				}else {
					hotMap.put(StringUtil.reSpecial(content),imageName);
				}
			}			
		}
		MysqlLocalConnect.close();
		return hotMap;
	}
	
	private Map<String, String> parseBSHotContent(String contentHTML,Map<String, String> hotMap) {
		if(contentHTML==null||"".equals(contentHTML)) return hotMap;
		Document doc = Jsoup.parse(contentHTML);  
		Elements links = doc.select("[class=j-r-list-c]");  
		String linkOuterH = null;  
		Document doc1 = null; 
		Elements links1 = null; 
		String content = null; 
		Elements imageEle = null;   
		String imagePath = null;
	  	MysqlLocalConnect.getConnect();
		for (Element element : links) {
			linkOuterH = element.outerHtml();  
			doc1 = Jsoup.parse(linkOuterH);  
			links1 = doc1.select("[class=j-r-list-c-desc]");  
			content = links1.text().trim();
			imageEle = doc1.select("img"); 
			imagePath = imageEle.attr("data-original").trim();
			String imageName = "";
			if(StringUtil.isNotEmpty(imagePath)) {
				imageName = imagePath.substring(imagePath.lastIndexOf("/")+1, imagePath.length());
			}
			//下载文件  并保存到本地，下载的时候去重这样保证下载的都是新数据！后面上传到服务器的时候不用再去重
			if (StringUtil.check(content)&&MysqlLocalConnect.query(content, imageName)) {
				if(StringUtil.isNotEmpty(imagePath)) {
					boolean a = DownloadUtil.downloadImg(imagePath,Const.hotPath+ymd+Const.o+imageName);
					if(a) hotMap.put(StringUtil.reSpecial(content),imageName);
				}else {
					hotMap.put(StringUtil.reSpecial(content),imageName);
				}
			}			
		}
		MysqlLocalConnect.close();
		return hotMap;
	}
	
}
